package servlet;

import pojo.Grade;
import pojo.Role;
import pojo.Student;
import pojo.Users;
import service.RoleService;
import service.RoleServiceImpl;
import service.UsersService;
import service.UsersServicesImpl;
import utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(urlPatterns = ("/user/userServlet"))
public class UserServlet extends HttpServlet {
    UsersService usersService = new UsersServicesImpl();
    RoleService roleService = new RoleServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("insert".equals(method)){
            addUser(req,resp);
        }else if ("update".equals(method)){
            updateUser(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }else if ("findlist".equals(method)){
            findlist(req, resp);
        }else if ("rolelist".equals(method)){
            rolelist(req,resp);
        }else {
            findById(req,resp);
        }
    }
    //查询列表
    protected void findlist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Users> usersList = null;
        PageUtils pageUtils = new PageUtils();
        //模糊查询
        //分页数据
        //页码值
        String pageIndex = req.getParameter("pageIndex");
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);
        try {
            usersList = usersService.selectAll(index, pageUtils.getPageSize());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //获取总页数
        int totalPage = 0;
        try {

            totalPage = usersService.total()%5>0?
                    usersService.total()/5+1:
                    usersService.total()/5;
            req.setAttribute("total",usersService.total());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //System.out.println(usersList);
        req.setAttribute("usersList",usersList);
        req.setAttribute("pageIndex",index);
        req.setAttribute("totalPage",totalPage);
        req.getRequestDispatcher("/power/user/list.jsp").forward(req,resp);
    }
    //查询role列表
    protected  void rolelist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<Role> roleList = roleService.selectALl(1,1000);
        req.setAttribute("roleList",roleList);
        req.getRequestDispatcher("/power/user/add.jsp").forward(req,resp);
    }
    //增加用户
    protected  void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String roleId = req.getParameter("roleId");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");
        Users user = new Users();
        user.setLoginName(loginName);
        user.setPassword(password);
        user.setRealName(realName);
        user.setSex(Integer.parseInt(sex));
        user.setRoleId(Integer.parseInt(roleId));
        user.setEmail(email);
        user.setPhone(phone);
        user.setAddress(address);
        user.setCardId(cardId);
        user.setDesc(desc);
        int i = usersService.addUser(user);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('新增成功');location.href='/user/userServlet?method=findlist'</script>");
        }else {
            writer.println("<script>alert('新增失败');location.href='/user/userServlet?method=rolelist'</script>");
        }
    }
    //修改用户
    protected void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String roleId = req.getParameter("roleId");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");
        String userid = req.getParameter("userid");
        Users user = new Users();
        user.setLoginName(loginName);
        user.setPassword(password);
        user.setRealName(realName);
        user.setSex(Integer.parseInt(sex));
        user.setRoleId(Integer.parseInt(roleId));
        user.setEmail(email);
        user.setPhone(phone);
        user.setAddress(address);
        user.setCardId(cardId);
        user.setDesc(desc);
        user.setUserid(Integer.parseInt(userid));
        int i = usersService.updateUser(user);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('更新成功');location.href='/user/userServlet?method=findlist'</script>");
        }else {
            writer.println("<script>alert('更新失败');location.href='/user/userServlet?userid="+userid+"'</script>");
        }
    }
    //删除用户
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String userid = req.getParameter("userid");
        int i = usersService.deleteById(Integer.parseInt(userid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('删除成功');location.href='/user/userServlet?method=findlist'</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/user/userServlet?method=findlist‘</script>");
        }
    }
    //根据id查询用户
    protected  void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String userid = req.getParameter("userid");
        System.out.println(userid);
        Users user = usersService.selectById(Integer.parseInt(userid));
        List<Role> roleList = roleService.selectALl(1,1000);
        //System.out.println(111);
        req.setAttribute("user",user);
        req.setAttribute("roleList",roleList);
        req.getRequestDispatcher("/power/user/edit.jsp").forward(req,resp);
    }
}

